﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp2
{
    public class Monster {
        /// <summary>
        /// 怪物移动路径的几个目标点
        /// </summary>
        private List<Point> Routes = new List<Point>{
            new Point(8,2),
            new Point(10,2),
            new Point(10,17),
            new Point(8,17),
            new Point(8,15),
            new Point(3,15),
            new Point(3,18),
            new Point(1,18),
            new Point(1,15),
            new Point(1,11),
            new Point(2,11),
            new Point(2,6),
            new Point(1,6),
            new Point(1,1),
            new Point(5,1),
            new Point(5,4),
            new Point(8,4),
            
        };
        //怪物当前的目标点
        private int CurrentDestnation { get; set; }
        //获取怪物当前坐标
        public Point Location { get; set; }
        //下次移动时间
        private DateTime NextMoveTime { get; set; }
        //初始化monster
        public Monster(int currentDestnation)
        {
            CurrentDestnation = currentDestnation;
            Location = Routes[currentDestnation];
            NextMoveTime = System.DateTime.Now.AddMilliseconds(100);
        }
        //坐标移动速度
        private Point speed {
            get {
                int PriorPoint = CurrentDestnation == 0 ? Routes.Count - 1 : CurrentDestnation - 1;
                int dx = Routes[CurrentDestnation].X - Routes[PriorPoint].X;//x移动速度
                int dy = Routes[CurrentDestnation].Y - Routes[PriorPoint].Y;//y移动速度
                return new Point(dx==0?0:dx/Math.Abs(dx),dy==0?0:dy/Math.Abs(dy));//返回怪物新坐标
            }
        }

        public Point? Move()
        {
            //没到下次移动时间什么都不做
            if (NextMoveTime>System.DateTime.Now)
            {
                return null;
            }
            //到达当前的目标点
            if (Location == Routes[CurrentDestnation])
            {
                //get the goal,move to next
                CurrentDestnation += 1;
                if (CurrentDestnation>=Routes.Count)
                {
                    CurrentDestnation = 0;
                }
            }
            else {
                Location = new Point(Location.X + speed.X, Location.Y+speed.Y);
            }
            NextMoveTime = System.DateTime.Now.AddMilliseconds(100);


            return Location;
        }

    }
}
